const wx = require('weixin-js-sdk')
import { share } from '@common/api/common'
import { sendActionMsg } from '@utils/common'

export function InitShare () {
	const promise = new Promise((resolve, reject) => {
		share({
			url: window.location.href.split('#')[0]
		}).then(res => {
			var json_res = JSON.parse(res.data)
			wx.config({
				debug: false,
				appId: json_res.appId,
				timestamp: json_res.timestamp,
				nonceStr: json_res.nonceStr,
				signature: json_res.signature,
				jsApiList: json_res.jsApiList,
				openTagList:["wx-open-launch-weapp","wx-open-launch-app"]
			})
			resolve('success')
		})
	})
	return promise
}


export function weixinShare(msg){
	wx.ready(function () {
	  wx.onMenuShareTimeline({
		title: msg.timelineTitle ? msg.timelineTitle : msg.title,
		link: msg.link,
		imgUrl: msg.image,
		desc: msg.timelineDesc ? msg.timelineDesc : msg.desc,
		success: function () {
			console.log('分享')
			sendActionMsg('首页点击“分享”按键','其他行为','H5_click_tool')
		},
		cancel: function () {
			console.log('分享')
			sendActionMsg('首页点击“分享”按键','其他行为','H5_click_tool')
		}
	  });
	  wx.onMenuShareAppMessage({
		title: msg.title,
		link: msg.link,
		imgUrl: msg.image,
		desc: msg.desc,
		success: function () {
			console.log('分享')
			sendActionMsg('首页点击“分享”按键','其他行为','H5_click_tool')
		},
		cancel: function () {
			console.log('分享')
			sendActionMsg('首页点击“分享”按键','其他行为','H5_click_tool')
		}
	  });
	  wx.onMenuShareQQ({
		title: msg.title,
		link: msg.link,
		imgUrl: msg.image,
		desc: msg.desc,
		success: function () {
			console.log('分享')
			sendActionMsg('首页点击“分享”按键','其他行为','H5_click_tool')
		},
		cancel: function () {
			console.log('分享')
			sendActionMsg('首页点击“分享”按键','其他行为','H5_click_tool')
		}
	  });
	  wx.onMenuShareWeibo({
		title: msg.title,
		link: msg.link,
		imgUrl: msg.image,
		desc: msg.desc,
		success: function () {
			console.log('分享')
			sendActionMsg('首页点击“分享”按键','其他行为','H5_click_tool')
		},
		cancel: function () {
			console.log('分享')
			sendActionMsg('首页点击“分享”按键','其他行为','H5_click_tool')
		}
	  });
	})
  }